package Timesheet;


import Timesheet.util.TsCalendar;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

import oracle.adfmf.java.beans.PropertyChangeListener;
import oracle.adfmf.java.beans.PropertyChangeSupport;


public class TimesheetPeriodReport {
    //
    private boolean changed = false;
    //
    private int sheetID = 0;
    private int sheetSeq = -1;
    private String periodType = "";
    private int periodSeq = 0;
    private Date startDt;
    private Date endDt;

    //
    private String reportedStatus = "";
    private String creationOprid = "";
    private Date creationDt;
    private String submissionOprid = "";
    private Date submissionDt;
    private String approverOprid = "";
    private Date approvalActionDt;
    private String lastUpdOprid = "";
    private Date lastUpdDt;
    private String rejectionReason = "";
    private String comment = "";
    private double scheduledDailyQty = 8.0; //TODO

    //
    private double totalReportedQuantity = 0.0;
    private double totalScheduledQuantity = 0.0;

    /**
     * @associates <{Timesheet.TimesheetDailyReport}>
     */
    private ArrayList tsDailyReportList = new ArrayList();
    private transient PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(this);

    public void cloneFrom(TimesheetPeriodReport srcPdReport) { //TODO
    }

    public void submit() { //TODO
    }

    public void save() { //TODO
    }


    public TimesheetPeriodReport() {
        super();
    }

    public TimesheetPeriodReport(int sheetID, int sheetSeq, TsCalendar calendar, String reportedStatus) {
        super();
        this.setSheetID(sheetID);
        this.setSheetSeq(sheetSeq);
        this.setPeriodType(calendar.getPeriodType());
        this.setPeriodSeq(calendar.getPeriodSeq());
        this.setStartDt(calendar.getStartDate());
        this.setEndDt(calendar.getEndDate());
        this.setReportedStatus(reportedStatus);
        ArrayList dailyRptList = new ArrayList();
        for (Date dt = this.startDt; dt.compareTo(this.endDt) <= 0; ) {
            dailyRptList.add(new TimesheetDailyReport(this.sheetID, this.sheetSeq, this.periodSeq, dt,
                                                      this.reportedStatus, this.scheduledDailyQty, 0, this.comment));
            // increase dt by 1 day
            Calendar c = Calendar.getInstance();
            c.setTime(dt);
            c.add(Calendar.DATE, 1);
            dt = c.getTime();
        }
        this.setTsDailyReportList(dailyRptList);
        this.setTotalScheduledQuantity(this.getTotalScheduledQuantity());
        this.setTotalReportedQuantity(this.getTotalReportedQuantity());
    }

    public TimesheetPeriodReport(int sheetID, int sheetSeq, String periodType, int periodSeq, Date startDt, Date endDt,
                                 String reportedStatus, Date creationDt, Date submissionDt, String approverOprid,
                                 Date approvalActionDt, String rejectionReason, String comment) {
        super();
        this.sheetID = sheetID;
        this.sheetSeq = sheetSeq;
        this.periodType = periodType;
        this.periodSeq = periodSeq;
        this.startDt = startDt;
        this.endDt = endDt;
        this.reportedStatus = reportedStatus;
        this.creationDt = creationDt;
        this.submissionDt = submissionDt;
        this.approverOprid = approverOprid;
        this.approvalActionDt = approvalActionDt;
        this.rejectionReason = rejectionReason;
        this.comment = comment;
    }

    /**
     * @User Defined
     */
    public TimesheetDailyReport[] getTsDailyReportList() {
        return (TimesheetDailyReport[])tsDailyReportList.toArray(new TimesheetDailyReport[tsDailyReportList.size()]);
    }


    public void addPropertyChangeListener(PropertyChangeListener l) {
        propertyChangeSupport.addPropertyChangeListener(l);
    }

    public void removePropertyChangeListener(PropertyChangeListener l) {
        propertyChangeSupport.removePropertyChangeListener(l);
    }

    public void setPeriodType(String periodType) {
        String oldPeriodType = this.periodType;
        this.periodType = periodType;
        propertyChangeSupport.firePropertyChange("periodType", oldPeriodType, periodType);
    }

    public String getPeriodType() {
        return periodType;
    }

    public void setPeriodSeq(int periodSeq) {
        int oldPeriodSeq = this.periodSeq;
        this.periodSeq = periodSeq;
        propertyChangeSupport.firePropertyChange("periodSeq", oldPeriodSeq, periodSeq);
    }

    public int getPeriodSeq() {
        return periodSeq;
    }

    public void setStartDt(Date startDt) {
        Date oldStartDt = this.startDt;
        this.startDt = startDt;
        propertyChangeSupport.firePropertyChange("startDt", oldStartDt, startDt);
    }

    public Date getStartDt() {
        return startDt;
    }

    public void setEndDt(Date endDt) {
        Date oldEndDt = this.endDt;
        this.endDt = endDt;
        propertyChangeSupport.firePropertyChange("endDt", oldEndDt, endDt);
    }

    public Date getEndDt() {
        return endDt;
    }

    public void setReportedStatus(String reportedStatus) {
        String oldReportedStatus = this.reportedStatus;
        this.reportedStatus = reportedStatus;
        propertyChangeSupport.firePropertyChange("reportedStatus", oldReportedStatus, reportedStatus);
    }

    public String getReportedStatus() {
        return reportedStatus;
    }

    public void setCreationDt(Date creationDt) {
        Date oldCreationDt = this.creationDt;
        this.creationDt = creationDt;
        propertyChangeSupport.firePropertyChange("creationDt", oldCreationDt, creationDt);
    }

    public Date getCreationDt() {
        return creationDt;
    }

    public void setSubmissionDt(Date submissionDt) {
        Date oldSubmissionDt = this.submissionDt;
        this.submissionDt = submissionDt;
        propertyChangeSupport.firePropertyChange("submissionDt", oldSubmissionDt, submissionDt);
    }

    public Date getSubmissionDt() {
        return submissionDt;
    }

    public void setTotalReportedQuantity(double totalReportedQuantity) {
        double oldTotalReportedQuantity = this.totalReportedQuantity;
        this.totalReportedQuantity = totalReportedQuantity;
        propertyChangeSupport.firePropertyChange("totalReportedQuantity", oldTotalReportedQuantity,
                                                 totalReportedQuantity);
    }

    /**
     * @User Defined
     */
    public double getTotalReportedQuantity() {
        this.totalReportedQuantity = 0;
        for (int i = 0; i < this.tsDailyReportList.size(); i++) {
            this.totalReportedQuantity += this.getTsDailyReportList()[i].getTotalReportedQuantity();
        }
        return this.totalReportedQuantity;
    }

    public void setTotalScheduledQuantity(double totalScheduledQuantity) {
        double oldTotalScheduledQuantity = this.totalScheduledQuantity;
        this.totalScheduledQuantity = totalScheduledQuantity;
        propertyChangeSupport.firePropertyChange("totalScheduledQuantity", oldTotalScheduledQuantity,
                                                 totalScheduledQuantity);
    }

    /**
     * @User Defined
     */
    public double getTotalScheduledQuantity() {
        this.totalScheduledQuantity = 0;
        for (int i = 0; i < this.tsDailyReportList.size(); i++) {
            this.totalScheduledQuantity += this.getTsDailyReportList()[i].getScheduledQuantity();
        }
        return this.totalScheduledQuantity;
    }

    public void setTsDailyReportList(ArrayList tsDailyReportList) {
        ArrayList oldTsDailyReportList = this.tsDailyReportList;
        this.tsDailyReportList = tsDailyReportList;

        this.setTotalReportedQuantity(getTotalReportedQuantity());
        this.setTotalScheduledQuantity(getTotalScheduledQuantity());

        propertyChangeSupport.firePropertyChange("tsDailyReportList", oldTsDailyReportList, tsDailyReportList);
    }

    public void setSheetSeq(int sheetSeq) {
        int oldSheetSeq = this.sheetSeq;
        this.sheetSeq = sheetSeq;
        propertyChangeSupport.firePropertyChange("sheetSeq", oldSheetSeq, sheetSeq);
    }

    public int getSheetSeq() {
        return sheetSeq;
    }

    public void setSheetID(int sheetID) {
        this.sheetID = sheetID;
    }

    public int getSheetID() {
        return sheetID;
    }

    public void setCreationOprid(String creationOprid) {
        String oldCreationOprid = this.creationOprid;
        this.creationOprid = creationOprid;
        propertyChangeSupport.firePropertyChange("creationOprid", oldCreationOprid, creationOprid);
    }

    public String getCreationOprid() {
        return creationOprid;
    }

    public void setSubmissionOprid(String submissionOprid) {
        String oldSubmissionOprid = this.submissionOprid;
        this.submissionOprid = submissionOprid;
        propertyChangeSupport.firePropertyChange("submissionOprid", oldSubmissionOprid, submissionOprid);
    }

    public String getSubmissionOprid() {
        return submissionOprid;
    }

    public void setApproverOprid(String approverOprid) {
        String oldApproverOprid = this.approverOprid;
        this.approverOprid = approverOprid;
        propertyChangeSupport.firePropertyChange("approverOprid", oldApproverOprid, approverOprid);
    }

    public String getApproverOprid() {
        return approverOprid;
    }

    public void setApprovalActionDt(Date approvalActionDt) {
        Date oldApprovalActionDt = this.approvalActionDt;
        this.approvalActionDt = approvalActionDt;
        propertyChangeSupport.firePropertyChange("approvalActionDt", oldApprovalActionDt, approvalActionDt);
    }

    public Date getApprovalActionDt() {
        return approvalActionDt;
    }

    public void setLastUpdOprid(String lastUpdOprid) {
        String oldLastUpdOprid = this.lastUpdOprid;
        this.lastUpdOprid = lastUpdOprid;
        propertyChangeSupport.firePropertyChange("lastUpdOprid", oldLastUpdOprid, lastUpdOprid);
    }

    public String getLastUpdOprid() {
        return lastUpdOprid;
    }

    public void setLastUpdDt(Date lastUpdDt) {
        Date oldLastUpdDt = this.lastUpdDt;
        this.lastUpdDt = lastUpdDt;
        propertyChangeSupport.firePropertyChange("lastUpdDt", oldLastUpdDt, lastUpdDt);
    }

    public Date getLastUpdDt() {
        return lastUpdDt;
    }

    public void setRejectionReason(String rejectionReason) {
        String oldRejectionReason = this.rejectionReason;
        this.rejectionReason = rejectionReason;
        propertyChangeSupport.firePropertyChange("rejectionReason", oldRejectionReason, rejectionReason);
    }

    public String getRejectionReason() {
        return rejectionReason;
    }

    public void setComment(String comment) {
        String oldComment = this.comment;
        this.comment = comment;
        propertyChangeSupport.firePropertyChange("comment", oldComment, comment);
    }

    public String getComment() {
        return comment;
    }

    public void setChanged(boolean changed) {
        this.changed = changed;
    }

    public boolean isChanged() {
        return changed;
    }
}
